<template>
    <div class="infoadd">
       <nav>
           <van-nav-bar
                title="新增收货地址"
                left-arrow
                @click-left="onClickLeft"
                />
       </nav>

       <main>
         <van-address-edit
            :area-list="areaList"
            :address-info="addressList"
            :show-delete="idx!=-1"
            show-set-default
            show-search-result
            :search-result="searchResult"
            :area-columns-placeholder="['请选择', '请选择', '请选择']"
            @save="onSave"
            @delete="onDelete"
            @change-area="handleArea"
           
            />
       </main>
     
    </div>
</template>

<script>
    import Vue from 'vue';
    import { Toast } from 'vant';
    import { areaList } from '@vant/area-data';
    export default {
        data() {
            return {
                idx:location.hash.split("/")[2],
                areaList,
                searchResult: [],
                addressList:{
                    id:'1',
                    name:'小黄',
                    tel:'13500000000',
                    province:'四川省',
                    city:'成都市',
                    county:'成华区',
                    addressDetail:'万象城50号',
                    address:'四川省成都市成华区万象城50号',
                    postalCode:'610051',
                    isDefault: false,
                    areaCode:'510108'
                }
               
            }
        },
        methods: {
            onClickLeft() {
            this.$router.go(-1)
            },
            // 保存
            onSave(obj) {
                let idx=location.hash.split("/")[2]
                Toast('保存成功');
                let {address}=this
                address=obj.province+obj.city+obj.county+obj.addressDetail
                if(idx==-1){ //新增保存
                   this.$store.commit('address/addressMut',{obj,address})
                }else{ //编辑保存
                    this.$store.commit('address/addressReplaceMut',{obj,idx,address}) 
                }
               
                
                this.$router.push({
                 path: "/address"})
            },
                // 删除
            onDelete(obj) {
               let idx=location.hash.split("/")[2]
                if(idx==-1){
                  this.$router.go(-1) //路由转换
                }else{
                  this.$store.commit('address/addressDelMut',idx)  //methods可以拿computed里的值
                  this.$router.go(-1) //路由转换
                }    
            },

            handleArea(obj){
              this.areaCode = obj[2].code
            }
            
            },

            computed: {
                addressEdit() {
                    return this.$store.state.address.addressList
                },

                
            },

             created () {
                
                  if(this.idx==-1){
                    this.addressList={};
                       console.log(2)
                 }else{
                     console.log(1)
                    this.addressList=this.addressEdit[this.idx] 
                 }
                  
                  
               
               
             },

           
     }
</script>

<style src="../assets/css/infoadd.css"  scoped>

</style>